#include "sensor1worker.h"
#include "qthread.h"
#include <QDateTime>

Sensor1Worker::Sensor1Worker(ushort di, ushort d0, QObject *parent)
    : SensorWorker(di, d0, parent) {}

void Sensor1Worker::stop()
{
    m_running = false;
}

void Sensor1Worker::process()
{
    clearInputEdge();

    while (m_running) {
        ushort rising_edge_count = 0;
        if (checkInputEdge(rising_edge_count)) {
            handleRisingEdge();
            clearInputEdge();
        }
        QThread::msleep(1);
    }
    emit finished();
}

void Sensor1Worker::handleRisingEdge()
{
    emit log("[" + QDateTime::currentDateTime().toString() +
             "] [DI]" + QString::number(m_di) + " : Rising edge trigger");

    // 输出200ms脉冲
    setTimedOutput(0, 200);
    emit log("[" + QDateTime::currentDateTime().toString() +
             "] [DO]" + QString::number(m_do) + " : Output 200ms pulse");
}
